class Json_class(dict):
    def  __init__(self, name):
        self.update({"name":name})
        return None                                ####初始化，只能返回None
    def add_children(self,children):
        
            self.update({"children":children})
            return self
    def graph_viz(self,g):
        g.node(self['name'])
        try:
            self['children']
            if type(self['children'])==list:
                [g.edge(self['name'],x['name']) for x in self['children'] ]
                return [x.graph_viz(g) for x in  self['children']]
            else:
                g.edge(self['name'],self['children']['name'])

                return self['children'].graph_viz(g)
        except:
            return g
        

from graphviz import Digraph

tom=Json_class("tom")
bob=Json_class("bob")
david=Json_class("david")
Ken=Json_class('Ken')

tom.add_children(bob)

bob.add_children(david)

david.add_children(Ken)

g = Digraph('test_digraph')
tom.graph_viz(g)

bob.add_children([Ken,david])


g = Digraph('test_digraph')
tom.graph_viz(g)
g

morphy=Json_class("morphy")
bene=Json_class("bene")
Ken.add_children([morphy,bene])

mao=Json_class('mao')
mao.add_children([bob,david])

g = Digraph('test_digraph')
mao.graph_viz(g)
g

chain=[]
for i in range(10):
    name=str(i)
    temp = Json_class(name)
    chain.append(temp)

chain



